#!/bin/bash
#安装脚本
#help命令返回文本
echo_help_txt(){
    echo -e "--------------------easywecom安装脚本使用指南----------------------------------------"
    echo -e ""
    echo -e "升级版本: sh update.sh -v=\${版本号}"
    echo -e "安装最简demo(release版): sh init.sh"
    echo -e "安装包含会话文档的版本: sh init.sh -type=enhance"
}
#docker环境安装
install_docker(){       
    echo "关闭防火墙"
    systemctl stop firewalld && systemctl disable firewalld

    echo "关闭selinux"
    sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g"  /etc/selinux/config
    setenforce 0

    echo "卸载docker相关组件"
    sudo yum remove -y  docdocker-client \
	docker-client \
	docker-client-latest \
	docker-common \
	docker-latest \
	docker-latest-logrotate \
	docker-logrotate \
	docker-selinux \
	docker-engine-selinux \
	docker-engine

    echo "安装相关组件"
    sudo yum install -y yum-utils \
        device-mapper-persistent-data \
	lvm2


    cat << 'EOF' > /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
#baseurl=https://download.docker.com/linux/centos/7/$basearch/stable
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=0
EOF

    echo "安装docker-ce"
    yum install -y docker-ce.x86_64
    echo "配置加速"
    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}
EOF
	
    sudo systemctl daemon-reload
    sudo systemctl restart docker

    sleep 2
}
#基础环境安装
install_base(){       
    echo "安装docker-compose"
    cp ./docker-compose  /usr/bin/docker-compose
    chmod +x /usr/bin/docker-compose

    echo "设置定时清理脚本"
    cp ./clearlog.sh /etc/clearlog.sh
    chmod +x /etc/clearlog.sh
    echo '0 3 * * * root /etc/clearlog.sh' >> /etc/crontab
    #新建docker网络
    echo "新建docker网络中。。"
    docker network create wecomscrm
    docker network inspect wecomscrm
    sleep 2
}



install_release()
{
    #无docker环境时
    if ! type docker >/dev/null 2>&1; then
        install_docker 
    fi
    install_base #安装基础环境
    [ ! -f "./.env" ]&&touch ./.env
    #根据版本进行服务模块安装
    sed -i '/ver_tag/d' .env
    echo ver_tag="$1">>.env
    if [[ $2 == 'internal' ]];then
        [ $4 -eq 0 ]&&docker-compose -f ./docker-compose-redis.yml up -d
        [ $4 -eq 1 ]&&docker-compose -f ./docker-compose-redis-ref.yml up -d
	fi
	
    if [[ $3 == 'internal' ]];then
        [ -d ./backup ]	|| mkdir ./backup
        [ $4 -eq 0 ]&&docker-compose -f ./docker-compose-mysql.yml up -d
        [ $4 -eq 1 ]&&docker-compose -f ./docker-compose-mysql-ref.yml up -d
	    #获取当前安装目录
        path_now=`pwd`
        path_mysql=$path_now"/mysql"
        path_chg=${path_mysql//"/"/"\/"}

        #修改数据库备份脚本并添加到定时任务
        sed -i s/install_path/$path_chg/g ./backupsql.sh
        chmod +x ./backupsql.sh
        cp ./backupsql.sh /etc/backupsql.sh
        echo '0 4 * * * root /etc/backupsql.sh' >> /etc/crontab
	fi
	sleep 10
    docker-compose -f ./docker-compose-service.yml up -d
    echo "安装结束"
}

install_enhance()
{
    #无docker环境时
    if ! type docker >/dev/null 2>&1; then
        install_docker 
    fi
    install_base     #安装基础环境
    [ ! -f "./.env" ]&&touch ./.env
    #根据版本进行服务模块安装
    sed -i '/ver_tag/d' .env
    echo ver_tag="$1">>.env
    es_address=`cat ./env_file.env | grep ES_ADDRESS`
    if [[ -z $es_address ]];then
        mkdir ./elastic
        chmod 777 -R ./elastic
        docker-compose -f ./docker-compose-es.yml up -d
        sed -i '/ES_OPEN/d' ./env_file.env
        echo 'ES_OPEN=true'>>./env_file.env
        echo "设置定时清理脚本"
        cp ./deleteins.sh /etc/deleteins.sh
        chmod +x /etc/deleteins.sh
        echo '0 3 * * * root /etc/deleteins.sh' >> /etc/crontab

    fi
    if [[ $2 == 'internal' ]];then
        [ $4 -eq 0 ]&&docker-compose -f ./docker-compose-redis.yml up -d
        [ $4 -eq 1 ]&&docker-compose -f ./docker-compose-redis-ref.yml up -d
	fi
    if [[ $3 == 'internal' ]];then
        [ -d ./backup ]	|| mkdir ./backup	
        [ $4 -eq 0 ]&&docker-compose -f ./docker-compose-mysql.yml up -d
        [ $4 -eq 1 ]&&docker-compose -f ./docker-compose-mysql-ref.yml up -d
	    #获取当前安装目录
        path_now=`pwd`
        path_chg=${path_now//"/"/"\/"}

        #修改数据库备份脚本并添加到定时任务
        sed -i s/install_path/$path_chg/g ./backupsql.sh
        chmod +x ./backupsql.sh
        cp ./backupsql.sh /etc/backupsql.sh
        echo '0 4 * * * root /etc/backupsql.sh' >> /etc/crontab
	fi
    sleep 5
    docker-compose -f docker-compose-service.yml up -d
    echo "安装结束"	
}


install_type='release'
ver='V1.29.0'
help_sign=0
reflect=0
redis_sign='internal'
mysql_sign='internal'
monitor_sign=0
var_num=$#
com='install'
for var in $@
do
index=`echo $var| awk -F"=" '{print $1}'`;
var1=`echo $var| awk -F"=" '{print $2}'`;
[ $index == "-v" ]&&ver=$var1;
[ $index == "-type" ]&&install_type=$var1;
[ $index == "-redis" ]&&redis_sign=$var1;
[ $index == "-mysql" ]&&help_sign=$var1;
[ $index == "--monitor" ]&&monitor_sign=1
done

if [[ $help_sign -eq 1 ]];then echo_help_txt;fi
[ $redis_sign != 'external' ]&&[ $redis_sign != 'internal' ]&&echo "redis初始化参数错误，请使用准确的参数"&&exit
[ $mysql_sign != 'external' ]&&[ $mysql_sign != 'internal' ]&&echo "mysql初始化参数错误，请使用准确的参数"&&exit

if [[ $com == 'install' ]]&&[[ $help_sign -eq 0 ]];then
    if [[ $install_type == 'release' ]];then
        install_release $ver $redis_sign $mysql_sign $reflect
    elif [[ $install_type == 'enhance' ]];then
        install_enhance $ver $mysql_sign $mysql_sign $reflect
    fi
    if [[ $monitor_sign -eq 1 ]];then
       IP=`curl cip.cc|awk -F" " 'NR==1 {print $3}'`
       sed -i '/host_ip/d' .env
       echo "host_ip="$IP>>.env
       docker-compose -f ./docker-compose-monitor.yml up -d
    fi
fi

